package com.nut.Filter;

import com.nut.common.Result;
import com.nut.utils.JsonUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class managerFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        httpServletRequest.setCharacterEncoding("UTF-8");

        httpServletResponse.setContentType("type/html;charset = UTF-8");

        String action = httpServletRequest.getParameter("action");
        if (action.equals("pageByName")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            Cookie[] cookies = httpServletRequest.getCookies();
            for(Cookie c: cookies){
                if (c.getName().equals("username")  && c.getValue() != null){
                    filterChain.doFilter(servletRequest,servletResponse);
                }
            }
        }

    }
    @Override
    public void destroy() {

    }
}